package cn.pacerx.running.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import cn.pacerx.running.db.dao.GoodsProductMapper;
import cn.pacerx.running.db.dao.GoodsProductMapper;
import cn.pacerx.running.db.dao.RunningGoodsProductMapper;
import cn.pacerx.running.db.domain.GoodsProduct;
import cn.pacerx.running.db.domain.GoodsProductExample;
import org.springframework.stereotype.Service;

@Service
public class GoodsProductService {
    @Resource
    private GoodsProductMapper goodsProductMapper;
    @Resource
    private RunningGoodsProductMapper runningGoodsProductMapper;

    public List<GoodsProduct> queryByGid(Integer gid) {
        GoodsProductExample example = new GoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return runningGoodsProductMapper.selectByExample(example);
    }

    public GoodsProduct findById(Integer id) {
        return runningGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        runningGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(GoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        runningGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
        GoodsProductExample example = new GoodsProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) runningGoodsProductMapper.countByExample(example);
    }

    public void deleteByGid(Integer gid) {
        GoodsProductExample example = new GoodsProductExample();
        example.or().andGoodsIdEqualTo(gid);
        runningGoodsProductMapper.logicalDeleteByExample(example);
    }

    public int addStock(Integer id, Short num){
        return goodsProductMapper.addStock(id, num);
    }

    public int reduceStock(Integer id, Short num){
        return goodsProductMapper.reduceStock(id, num);
    }

    public void updateById(GoodsProduct product) {
        product.setUpdateTime(LocalDateTime.now());
        runningGoodsProductMapper.updateByPrimaryKeySelective(product);
    }
}